<template>
  <div class="login">
    <el-container>
      <el-row>
        <!-- 点击图片跳转到首页 -->
        <el-link class="" @click="handleIndex" :underline="false"
          ><img
            style="height: 100px; width: 150px"
            src="../assets/miju2.jpg"
            alt=""
        /></el-link>
      </el-row>
    </el-container>
    <div class="wrapper">
      <el-container>
        <!--  登录表单 -->
        <el-form
          :model="formLogin"
          :rules="rules"
          ref="formLogin"
          style="margin-top: 20px"
        >
          <!-- 账号输入框 -->
          <el-form-item label="账号" prop="name" style="padding-top: 20px">
            <el-input placeholder="请输入帐号" v-model="formLogin.name" />
          </el-form-item>
          <!-- 密码输入框 -->
          <el-form-item v-if="visible" label="密码" prop="password">
            <el-input
              type="password"
              placeholder="请输入密码"
              v-model="formLogin.password"
            >
              <i
                slot="suffix"
                title="显示密码"
                @click="changePass('show')"
                style="cursor: pointer"
                class="el-input__icon iconfont icon-xianshi"
              ></i>
            </el-input>
          </el-form-item>
          <el-form-item v-else label="密码" prop="password">
            <el-input
              type="text"
              v-model="formLogin.password"
              placeholder="请输入密码"
            >
              <i
                slot="suffix"
                title="隐藏密码"
                @click="changePass('hide')"
                style="cursor: pointer"
                class="el-input__icon iconfont icon-yincang"
              ></i>
            </el-input>
          </el-form-item>
          <el-row style="text-align: center">
            <el-button type="primary" plain @click="login">登录</el-button>
          </el-row>
          <el-row>
            <el-col style="width: 50%; text-align: center"
              ><el-link type="primary" @click="toReg" style="width: 30%"
                >注册</el-link
              ></el-col
            >
            <el-col style="width: 50%; text-align: center"
              ><el-link type="primary" @click="resetForm" style="width: 30%"
                >重置</el-link
              ></el-col
            >
          </el-row>
          <el-row style="margin-top: 20px">
            <el-col :span="4" :offset="6"
              ><img
                @click="handleWeiBo()"
                style="height: 32px; width: 32px"
                src="../assets/weibo.png"
            /></el-col>
            <el-col :span="4" :offset="2"
              ><img style="height: 32px; width: 32px" src="../assets/qq.png"
            /></el-col>
            <el-col :span="4" :offset="2"
              ><img
                style="height: 32px; width: 32px"
                src="../assets/weixin.png"
            /></el-col>
          </el-row>
        </el-form>
      </el-container>
    </div>

    <!--  底部 -->
    <el-footer>
      <a href="#" target="_blank">简体</a><span>|</span>
      <a href="#" target="_blank">繁体</a><span>|</span>
      <a href="#" target="_blank">English</a><span>|</span>
      <a href="#" target="_blank">常见问题</a><span>|</span>
      <a href="#" target="_blank ">隐私政策</a>
      <p class="copyright">
        Copyright ©2020 miju.futurefe.com All Rights Reserved.
      </p>
    </el-footer>
  </div>
</template>
<script>
export default {
  data() {
    let checkUserName = (rule, value, cb) => {
      var reg = /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/; //正则验证账号
      if (!value) {
        return cb(new Error("账号不能为空!"));
      } else if (!reg.test(value)) {
        return cb(new Error("字母开头,长度5-16之间,允许字母数字下划线"));
      } else {
        cb(); // 将判断传递给后面
      }
    };
    let checkPassword = (rule, value, cb) => {
      if (!value) {
        return cb(new Error("密码不能为空!"));
      } else if (value.length < 6) {
        return cb(new Error("密码长度最小6位"));
      } else {
        cb();
      }
    };

    return {
      activeName: "first",
      //规则
      rules: {
        name: [
          {
            validator: checkUserName,
            trigger: "blur",
            required: true,
          },
        ],
        password: [
          {
            validator: checkPassword,
            trigger: "blur",
            required: true,
          },
        ],
      },
      formLogin: {
        name: "",
        password: "",
      },
      visible: true,
    };
  },
  methods: {
    changePass(value) {
      this.visible = !(value === "show");
    }, //判断渲染，true:暗文显示，false:明文显示

    resetForm() {
      //重置表单
      console.log("session");
      this.$refs["formLogin"].resetFields();
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
    toReg() {
      this.$router.push({
        path: "/reg",
      });
    },
    handleWeiBo() {
      console.log("我被执行了");
      window.open("http://42.194.142.165:8088/front/mi/api/weibo/login");
    },

    // 登录验证判断
    login() {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0)",
      });
      this.$refs.formLogin.validate((valid) => {
        if (valid) {
          //判断表单验证是否通过
          this.$axios
            .post("login", {
              username: this.formLogin.name,
              password: this.formLogin.password,
            })
            .then((response) => {
              if (response.data.meta.status === 200) {
                console.log(response);
                this.$message({
                  message: "登录成功",
                  type: "success",
                });
                window.sessionStorage.setItem(
                  "token",
                  response.data.data.token
                );
                loading.close();
                this.$router.push("/index"); //成功所跳转的页面
              } else {
                this.$message({
                  message: response.data.meta.msg,
                  type: "error",
                });
                loading.close();
                console.log(response);
                return false;
              }
            })
            .catch(function (error) {
              console.log(error);
              loading.close();
            });
        } else {
          this.$message({
            message: "账号或密码格式不正确，请重新填写",
            type: "error",
          });
          loading.close();
          return false;
        }
      });
    },
    handleIndex() {
      this.recordNum = "1";
      this.$emit("index", "1");
      this.$router.push("Index");
    },
  },
};
</script>
<style scoped>
.login {
  color: #333333;
  font-size: 12px;
  margin: 0;
}

.el-container {
  height: 98px;
  color: #333333;
  font-size: 12px;
  margin: 0;
  width: 100%;
  position: relative;
  margin-right: auto;
  margin-left: auto;
}

.el-container img {
  width: auto;
  height: 100%;
}

.wrapper {
  background: url("../assets/bg.jpg") no-repeat center;
}

.wrapper .el-container {
  height: 576px;

  color: #333333;
  font-size: 12px;
  margin: 0;
  padding: 0;
  outline: none;
  position: relative;
  width: 100%;
  margin-right: auto;
  margin-left: auto;
}

.wrapper .el-container .el-form {
  color: #333333;
  font-size: 12px;
  margin: 0;
  box-sizing: border-box;
  width: 32%;
  height: 400px;
  background-color: #ffffff;
  position: absolute;
  top: 50px;
  right: 20px;
  overflow: hidden;
  padding: 0 31px;
  border-radius: 10px;
}

.wrapper .el-container .el-form .el-form-item {
  margin-bottom: 14px;
}

.wrapper .el-container .el-form .el-button {
  width: 60%;
  line-height: 50px;
  margin-top: 10px;
  font-size: 16px;
  margin-left: 20px;
  padding: 0;
}

.wrapper .el-container .el-form .el-link {
  padding: 10px 50px 10px 60px;
}

.wrapper .el-container .el-form .tips .sms {
  color: #ff6600;
}

.wrapper .el-container .el-form .tips .reg {
  color: #999999;
}

.wrapper .el-container .el-form .tips .reg span {
  margin: 0 7px;
}

.wrapper .el-container .el-form .el-container img {
  width: 160px;
  height: 160px;
  background-color: #ccc;
  margin: 80px 80px;
}

.wrapper .el-container .el-form .el-container .beizhu {
  text-align: center;
  font-size: 13px;
  margin-top: 10px;
  color: #999;
}
.sm_login .el-container {
  height: auto;
  width: 50%;
  margin: 0 auto;
}
.sm_login .el-form {
  width: 100%;
}

.el-footer {
  height: 100px;
  padding-top: 60px;
  color: #999999;
  font-size: 16px;
  text-align: center;
}

.el-footer a {
  color: #999999;
  display: inline-block;
  text-decoration: none;
}

.el-footer .span {
  margin: 0 10px;
}

.el-footer .copyright {
  margin-top: 13px;
}
</style>
